package com.example.demo2.controller;

import com.alibaba.excel.EasyExcel;
import com.example.demo2.Util.ExcelUtil;
import com.example.demo2.generator.entity.R;
import com.example.demo2.generator.entity.UserMessage;
import com.example.demo2.generator.service.UserMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

@Controller
@Api(value = "用户信息 Controller",tags = "用户信息接口")
@RequestMapping("/UserMessage")
public class UserMessageController {
    @Autowired
    private UserMessageService userMessageService;


    @ApiOperation("查询所有用户")
    @RequestMapping(value = "/queryAllUserMessage",method = RequestMethod.GET)
    @ResponseBody
    public R queryAllUserMessage(){
        List<UserMessage> userMessages = userMessageService.queryAllUserMessage();
        System.out.println(userMessages);
        return R.ok(userMessages.size(),"查询成功",userMessages);
    }
    @ApiOperation("更新用户信息")
    @RequestMapping(value = "/UpdateUserMessage",method = RequestMethod.POST)
    @ResponseBody
    public R UpdateUserMessage(@ApiParam("UserMessage")@RequestBody UserMessage userMessage){
        System.out.println(".............");
        System.out.println(userMessage);
        UserMessage update = userMessageService.update(userMessage);
        return R.ok("更新成功",update);
    }
    @ApiOperation("删除用户信息")
    @RequestMapping(value = "/deleteUserMessage",method = RequestMethod.POST)
    @ResponseBody
    public R delUserMessage(@ApiParam(name = "UserMessage") UserMessage userMessage){
        userMessageService.deleteById(userMessage.getUserId());
        return R.ok("更新成功");
    }
    @ApiOperation("添加用户信息")
    @RequestMapping(value = "/AddUserMessage",method = RequestMethod.POST)
    @ResponseBody
    public R AddMessage(@ApiParam(name = "UserMessage") @RequestBody UserMessage userMessage){
        System.out.println("..........");
        System.out.println(userMessage.toString());
        System.out.println(userMessage.getStartDate());
        System.out.println("...........");
        userMessageService.insert(userMessage);
        return R.ok("更新成功");
    }
    @ApiOperation("根据条件用户信息")
    @RequestMapping(value = "/queryMessageByTerm",method = RequestMethod.POST)
    @ResponseBody
    public R queryMessageByTerm(@ApiParam(name = "userMessage",value = "用户基本信息") UserMessage userMessage){
        System.out.println("..........");
        System.out.println(userMessage.getStartDate());
        System.out.println("...........");
        List<UserMessage> userMessages = userMessageService.queryAllByTerm(userMessage);
        return R.ok(userMessages.size(),"更新成功",userMessages);
    }
    @ApiOperation("批量删除用户信息")
    @RequestMapping(value = "/delUserMessageBatch",method = RequestMethod.POST)
    @ResponseBody
    public R delUserMessageBatch(@RequestBody List<UserMessage>userMessages) throws UnsupportedEncodingException {
        System.out.println("................");
        System.out.println(Arrays.toString(userMessages.toArray()));
        userMessageService.delUserMessageBatch(userMessages);
        System.out.println("............");
        return R.ok();
    }
    @ApiOperation("导出所有用信息")
    @RequestMapping(value = "/dcAllUserMessage",method = RequestMethod.GET)
    @ResponseBody
    public R dcUserMessage(HttpServletResponse response) throws IOException {
        System.out.println(".....................................");
        System.out.println("导出。。。。。。。。。。。。。。。。。。。。。");
        System.out.println(".....................................");
        //设置响应头格式
        ExcelUtil.inputExcel(response,"用户信息");
        List<UserMessage> userMessages = userMessageService.queryAllUserMessage();
        ExcelUtil.getExcel(UserMessage.class,userMessages,"用户信息表",response);
        return R.ok();
    }
    @ApiOperation("导入所有用信息")
    @RequestMapping(value = "/drAllUserMessage",method = RequestMethod.POST)
    @ResponseBody
    public R drUserMessage(HttpServletResponse response) throws UnsupportedEncodingException {
        ExcelUtil.inputExcel(response,"用户信息表");
        return R.ok();
    }
}
